package com.ks.app.navigate.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.ks.app.admin.action.BaseAction;
import com.ks.app.admin.hibernate.KsVersion;
import com.ks.app.admin.service.KsVersionService;
import com.ks.app.admin.util.Pager;
import com.ks.app.navigate.hibernate.KsNavigate;
import com.ks.app.navigate.service.KsNavigateService;
import com.ks.app.navigate.util.JsonUtil;
import com.ks.app.navigate.util.JsoupUtil;

@ParentPackage("struts-default")
@InterceptorRef("defaultStack")
@Results( {
	@Result(name = "editnav", location = "/WEB-INF/navigate/editnav.jsp"),
	@Result(name = "listnav", location = "/WEB-INF/navigate/listnav.jsp")
})
public class KsNavigateAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	private Log log = LogFactory.getLog(this.getClass());
	
	@Resource(name="ksNavigateService")
	private KsNavigateService navService;
	
	@Resource(name="ksVersionService")
	private KsVersionService  verService;
	
	/**
	 * 导航条页面
	 * @return
	 */
	@Action(value="edit_nav")
	public String editnavigate(){
		
		long  id = request.getParameter("id")==null?0l:Long.parseLong(request.getParameter("id"));
		int pno  = request.getParameter("pno")==null?1:Integer.parseInt(request.getParameter("pno"));
		
		int  status    = request.getParameter("status")   == null?0:Integer.parseInt(request.getParameter("status"));
		long parentid  = (request.getParameter("parentid") == null || "".equals(request.getParameter("parentid")))?-1l:Long.parseLong(request.getParameter("parentid"));
		String name    = request.getParameter("name")     == null?"":request.getParameter("name").trim();
		
		if(id != 0){
			KsNavigate nav = navService.findKsNavigateById(id);
			request.setAttribute("nav", nav);
		}
		
		List<KsNavigate> list = navService.findParentKsNavigate();
		request.setAttribute("plist", list);
		request.setAttribute("pno", pno);
		request.setAttribute("name", name);
		request.setAttribute("parentid", parentid);
		request.setAttribute("status", status);
		
		return "editnav";
	}
	
	/**
	 * 后台----新增或修改导航条
	 * @return
	 */
	@Action(value="ks_add_navigate")
	public String addNavigate(){
		
		try {
			
			long          id =  request.getParameter("nid")==null?0l:Long.parseLong(request.getParameter("nid"));
			long    parentid =  request.getParameter("nparentid")==null?0l:Long.parseLong(request.getParameter("nparentid"));
			String     name  =  request.getParameter("nname")==null?"":request.getParameter("nname");
			String      url  =  request.getParameter("nurl")==null?"":request.getParameter("nurl");
			int   status     =  request.getParameter("nstatus")==null?1:Integer.parseInt(request.getParameter("nstatus"));
			int   ordernum  =  request.getParameter("nordernum")==null?1:Integer.parseInt(request.getParameter("nordernum"));
			
			KsNavigate  nav = null;
			if(id==0){
				 nav = new KsNavigate(parentid,name,url,status,ordernum);
			}else{
				 nav = navService.findKsNavigateById(id);
				 nav.setParentid(parentid);
				 nav.setName(name);
				 nav.setStatus(status);
				 nav.setUrl(url);
				 nav.setOrdernum(ordernum);
			}
			
			navService.saveKsNavigate(nav);	
		} catch (Exception e) {
			log.info(e.getMessage());
			e.printStackTrace();
			
		}
		return findNavigateList();
	}

	
	/**
	 * 后台----删除导航条，同时删除其下站点
	 * @return
	 */
	@Action(value="ks_del_navigate")
	public String delNavigate(){
		
		String usrname = String.valueOf(request.getSession().getAttribute("userinfo"));
	
		String ids = request.getParameter("id") == null?"":request.getParameter("id");
		navService.deleteKsNavigates(ids,usrname);	
		return findNavigateList();
		
	}

	/**
	 * 后台----导航条列表数据
	 * pno
	 * status
	 * parentid
	 * @return
	 */
	@Action(value="ks_navigate_grid")
	public String findNavigateList(){
		
		
		try {

			int  pno       = request.getParameter("pno")      == null?1:Integer.parseInt(request.getParameter("pno"));
			int  status    = request.getParameter("status")   == null?0:Integer.parseInt(request.getParameter("status"));
			long parentid  = (request.getParameter("parentid") == null || "".equals(request.getParameter("parentid")))?-1l:Long.parseLong(request.getParameter("parentid"));
			String name    = request.getParameter("name")     == null?"":request.getParameter("name").trim();
						
			KsNavigate k = new KsNavigate();
			k.setStatus(status);
			k.setParentid(parentid);
			k.setName(name);
			
			Pager pager = new Pager();
			pager.setCntOfPage(20);
			pager.setCurPage(pno);
			
			List<KsNavigate> list = navService.findKsNavigateList(name,parentid, status, pager);
			
			if(pno>1 && (list==null || list.size()==0)){
				
				pager.setCurPage(pno-1);
				list = navService.findKsNavigateList(name,parentid, status, pager);
				
			}else if(pno==1 && (list==null || list.size()==0)){
				pager.setCurPage(0);
			}
			
			List<KsNavigate> plist = navService.findParentKsNavigate();
			
			request.setAttribute("list", list);
			request.setAttribute("PAGER", pager);
			request.setAttribute("navigate", k);
			request.setAttribute("plist", plist);
			
		} catch (Exception e) {
			log.info(e.getMessage());
			e.printStackTrace();
		}
		
		return "listnav";
	}
	
	
	
	/**
	 * 抓取导航条
	 * @return
	 */
	@Action(value="initnavigate")
	public String initNavigate(){
		
		List<KsNavigate> list = JsoupUtil.crawlGuideType();
		for(KsNavigate n : list){
			navService.saveKsNavigate(n);
		}
		JsonUtil.outJson("{\"success\":true}", response);
		return null;
	}
	
	/**
	 * 更新导航信息，升级版本
	 * @return
	 */
	@Action(value="updatewnav")
	public String updatewnav(){
		
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		String version = sdf1.format(new Date());
		
		KsVersion wnav = new KsVersion();
		wnav.setCreatetime(sdf2.format(new Date()));
		wnav.setStatus(2);
		wnav.setVersion(version);
		wnav.setVtype(2);
		
		verService.addKsVersion(wnav, "");
		
		JsonUtil.outJson("{\"success\":true}", response);
		return null;
	}
	
	
	
}
